<template>
  <div>
    <a-card :bordered="false">
      <f-table ref="table" size="default" rowKey="audioId" :columns="columns" :multiple="true" :operator="operator" :params="queryParam" :api="api.audio">
         <span slot="sortOrder" slot-scope="text, record, index">
          <a-input :value="record['sortOrder']" @change="(event) =>sortOrderChange(event,record)" placeholder="排序号" style="width: 80px"/>
        </span>
        <span slot="audioUrl" slot-scope="text, record, index">
          <audio :src="record.audioUrl" controls="controls" style="height:20px"></audio>
        </span>
        <span slot="audioState" slot-scope="text, record, index">
          <a-switch v-model="record.audioState==1" checkedChildren="已发布" unCheckedChildren="未发布" @change="(checked) => setStatus(checked,record)"/>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="$refs.drawer.showDrawer(record)">编辑</a>
            <a-divider type="vertical"/>
          </template>
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay">
              <a-menu-item v-if="$auth('table.delete')">
                <a href="javascript:;" @click="$refs.table.del(record)">删除</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </f-table>
      <audio-drawer ref="drawer"></audio-drawer>
    </a-card>
  </div>
</template>
<script>
  import audioDrawer from './audio_drawer.vue'
  import api from './js/api'

  export default {
    name: 'TableList',
    components: {
      audioDrawer
    },
    data() {
      let me = this
      //操作按钮
      let operator = []
      operator.push({
        name: '添加',
        icon: 'plus',
        that: this,
        handler: function(table, that) {
          that.$refs.drawer.showDrawer()
        }
      })
      operator.push({
        name: '删除',
        icon: 'delete',
        batch: true,
        handler: function(table) {
          table.remove()
        }
      })
      return {
        api: api,
        //列表初始化查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '#',
            width: '60',
            align: 'center',
            customRender: (text, record, index) => index + 1
          },
          {
            title: '音频标题',
            align: 'center',
            dataIndex: 'audioTitle'
          },
          {
            title: '音频本地地址',
            align: 'center',
            scopedSlots: { customRender: 'audioUrl' }
          },
          {
            title: '排序号',
            align: 'center',
            scopedSlots: { customRender: 'sortOrder' }
          },
          {
            title: '发布状态',
            align: 'center',
            dataIndex: 'audioState',
            scopedSlots: { customRender: 'audioState' }
          },
          {
            title: '创建时间',
            align: 'center',
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            align: 'center',
            scopedSlots: { customRender: 'action' }
          }
        ],
        operator: operator
      }
    },
    created() {
    },
    methods: {
      setStatus(check, record) {
        this.http.post(this, {
          url: this.api.audio.setState,
          data: {
            ids: record.audioId,
            audioState: Number(check)
          },
          success(me, data) {
            record.audioState = data
          }
        })
      },
      sortOrderChange(event, record) {
        this.http.post(this, {
          url: this.api.audio.update,
          data: {
            id: record.audioId,
            sortOrder: event.target.value
          },
          success(me, data) {
            record.sortOrder = data.sortOrder
          }
        })
      }
    }
  }
</script>
